package com.core.common.manger.storage;

import cn.hutool.core.io.FileUtil;
import com.aliyun.oss.OSSClient;
import com.core.common.manger.MangerProperties;
import com.core.common.manger.storage.config.StorageAutoConfiguration;
import com.core.common.utils.MinioUtil;
import io.minio.*;
import io.minio.messages.Bucket;
import lombok.Data;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;

/**
 * @author hl
 * @version 1.0.0
 * @ClassName StorageMinio.java
 * @Description 类描述
 * @createTime 2023年05月31日 11:09:00
 */
@Slf4j
@Data
public class StorageMinio implements IStorageService {
    private String address;
    private String active="Minio";
    private String endpoint;
    private String accessKey;
    private String secretKey;
    private String bucketName;

    @Autowired
    private MinioClient minioClient;



    @SneakyThrows
    @Override
    public FileMate store(InputStream inputStream, long contentLength, String keyName) {
        FileMate mate=generateFileMateWith(null,keyName);
        String contextType=getContentTypeBy(keyName);

        FileUtil.writeFromStream(inputStream,mate.getFilePath());
        PutObjectArgs args = PutObjectArgs.builder()
                    .bucket(bucketName)
                    .object(mate.getFileKey())
                    .stream(inputStream, inputStream.available(), -1)
                    .contentType(contextType)
                    .build();

        ObjectWriteResponse response = minioClient.putObject(args);

        mate.setFileUrl(address+"/"+bucketName+"/"+mate.getFileKey());
        mate.setFileType(contextType);
        mate.setFileLength(contentLength);
        mate.setFileDir(null);
        mate.setFilePath(null);


        return mate;

    }



    @Override
    public Path load(String keyName) {
        return null;
    }

    @Override
    public Resource loadAsResource(String keyName) {
        return null;
    }

    @Override
    public void delete(String keyName) {

    }




}
